<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">电梯编码</label>
        <el-input v-model="query.liftNo" clearable placeholder="电梯编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">当前电梯楼层</label>
        <el-input v-model="query.liftFloor" clearable placeholder="当前电梯楼层" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">电梯状态</label>
        <el-input v-model="query.status" clearable placeholder="电梯状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :plain="true" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="liftNo" label="电梯编码" />
        <el-table-column prop="liftFloor" label="当前电梯楼层" />
        <el-table-column prop="liftAddress" label="该电梯通讯地址" />
        <el-table-column prop="status" label="电梯状态" />
        <el-table-column v-if="checkPer(['admin','wcsLift:edit','wcsLift:del'])" label="操作" width="180px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;gap: 5px;">
              <el-button type="success" size="mini" :loading="btn.open === scope.$index" plain icon="el-icon-cpu" @click="openDoor(scope.row, scope.$index)">开门</el-button>
              <el-button type="danger" size="mini" :loading="btn.close === scope.$index" plain icon="el-icon-cpu" @click="closeDoor(scope.row, scope.$index)">关门</el-button>
            </div>
            <!-- <udOperation
              :data="scope.row"
              :permission="permission"
            /> -->
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudWcsLift from '@/api/work/elevators'
import { closeDoorAPI, openDoorAPI } from '@/api/work/elevators'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, liftNo: null, liftFloor: null, liftAddress: null, status: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'Elevators',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'WcsLiftController', url: 'api/wcsLift', idField: 'id', sort: 'id,desc', crudMethod: { ...crudWcsLift },
      optShow: {
        edit: false,
        download: true,
        add: false,
        del: false,
        reset: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'wcsLift:add'],
        edit: ['wcsLift:edit'],
        del: ['wcsLift:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'liftNo', display_name: '电梯编码' },
        { key: 'liftFloor', display_name: '当前电梯楼层' },
        { key: 'status', display_name: '电梯状态' }
      ],
      btn: {
        close: '',
        open: ''
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async openDoor(row, index) {
      try {
        this.btn.open = index
        const submitForm = {
          door: 1,
          liftCode: row.liftNo,
          tag: 0
        }
        const res = await openDoorAPI(submitForm)
        if (res.code === 200) {
          this.$message.success(res.message)
          this.crud.toQuery()
        } else {
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.open = ''
      }
    },
    async closeDoor(row, index) {
      try {
        this.btn.close = index
        const res = await closeDoorAPI(row.liftNo)
        if (res.code === 200) {
          this.$message.success(res.message)
          this.crud.toQuery()
        } else {
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.close = ''
      }
    }
  }
}
</script>

<style scoped>

</style>
